package cn.wolfcode.web.controller;


import cn.wolfcode.domain.LogManage;
import cn.wolfcode.qo.LogQueryObject;
import cn.wolfcode.qo.UserContext;
import cn.wolfcode.service.ILogManageService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/log")
public class LogManageController {

    @Autowired
    private ILogManageService logManageService;


    @RequiredPermission(name="日志管理页面",expression="logManage:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") LogQueryObject qo, HttpServletResponse response) throws IOException {
        if(UserContext.getEmployee().isAdmin() == true){
            PageInfo<LogManage> pageInfo = logManageService.query(qo);
            model.addAttribute("pageInfo", pageInfo);
            return "log/log";
        }else {
            //response.sendRedirect("/nop");
            return "common/nopermission";
        }

    }

    @RequestMapping("/delete")
    @RequiredPermission(name="日志管理删除",expression="logManage:delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            logManageService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name="日志管理新增/编辑",expression="logManage:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(LogManage logManage){
        if (logManage.getId() != null) {
            logManageService.update(logManage);
        }else {
            logManageService.save(logManage);
        }
        return new JsonResult();
    }
}
